//---------------------------------------------------------------------------//
// Copyright (c) 2017-2021 Mikhail Komarov <nemo@nil.foundation>
// Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef MARSHALLING_TRANSPORT_FIELDS_ACCESS_HPP
#define MARSHALLING_TRANSPORT_FIELDS_ACCESS_HPP

#include <tuple>
#include <type_traits>

#include <nil/marshalling/detail/macro_common.hpp>
#include <nil/marshalling/detail/gen_enum.hpp>
#include <nil/marshalling/detail/base_detection.hpp>

#ifdef MARSHALLING_MUST_DEFINE_BASE
#define MARSHALLING_ACCESS_TRANSPORT_FIELD_FUNC(T_, t_, n_)                                                \
    typename std::tuple_element<MARSHALLING_CONCATENATE(TransportFieldIdx_, n_), typename Base::T_>::type& \
        MARSHALLING_CONCATENATE(transportField_, n_)()
#define MARSHALLING_ACCESS_TRANSPORT_FIELD_CONST_FUNC(T_, t_, n_)                                                \
    const typename std::tuple_element<MARSHALLING_CONCATENATE(TransportFieldIdx_, n_), typename Base::T_>::type& \
        MARSHALLING_CONCATENATE(transportField_, n_)() const
#define MARSHALLING_MSG_TRANSPORT_FIELDS_ACCESS_FUNC typename Base::transport_fields_type& transport_fields()
#define MARSHALLING_MSG_TRANSPORT_FIELDS_ACCESS_CONST_FUNC \
    const typename Base::transport_fields_type& transport_fields() const

#else    // #ifdef MARSHALLING_MUST_DEFINE_BASE
#define MARSHALLING_ACCESS_TRANSPORT_FIELD_FUNC(T_, t_, n_)            \
    FUNC_AUTO_REF_RETURN(MARSHALLING_CONCATENATE(transportField_, n_), \
                         decltype(std::get<MARSHALLING_CONCATENATE(TransportFieldIdx_, n_)>(t_)))
#define MARSHALLING_ACCESS_TRANSPORT_FIELD_CONST_FUNC(T_, t_, n_)            \
    FUNC_AUTO_REF_RETURN_CONST(MARSHALLING_CONCATENATE(transportField_, n_), \
                               decltype(std::get<MARSHALLING_CONCATENATE(TransportFieldIdx_, n_)>(t_)))
#define MARSHALLING_MSG_TRANSPORT_FIELDS_ACCESS_FUNC \
    FUNC_AUTO_REF_RETURN(transport_fields, decltype(nil::marshalling::to_message(*this).transport_fields()))
#define MARSHALLING_MSG_TRANSPORT_FIELDS_ACCESS_CONST_FUNC \
    FUNC_AUTO_REF_RETURN_CONST(transport_fields, decltype(nil::marshalling::to_message(*this).transport_fields()))
#endif    // #ifdef MARSHALLING_MUST_DEFINE_BASE

#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)                      \
    MARSHALLING_ACCESS_TRANSPORT_FIELD_FUNC(T_, t_, n_) {                     \
        return std::get<MARSHALLING_CONCATENATE(TransportFieldIdx_, n_)>(t_); \
    }                                                                         \
    MARSHALLING_ACCESS_TRANSPORT_FIELD_CONST_FUNC(T_, t_, n_) {               \
        return std::get<MARSHALLING_CONCATENATE(TransportFieldIdx_, n_)>(t_); \
    }

#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_1(T_, t_, n_) MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_2(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_1(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_3(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_2(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_4(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_3(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_5(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_4(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_6(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_5(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_7(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_6(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_8(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_7(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_9(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)            \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_8(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_10(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_9(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_11(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_10(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_12(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_11(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_13(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_12(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_14(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_13(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_15(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_14(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_16(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_15(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_17(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_16(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_18(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_17(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_19(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_18(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_20(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_19(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_21(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_20(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_22(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_21(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_23(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_22(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_24(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_23(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_25(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_24(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_26(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_25(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_27(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_26(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_28(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_27(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_29(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_28(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_30(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_29(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_31(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_30(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_32(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_31(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_33(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_32(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_34(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_33(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_35(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_34(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_36(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_35(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_37(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_36(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_38(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_37(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_39(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_38(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_40(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_39(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_41(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_40(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_42(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_41(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_43(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_42(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_44(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_43(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_45(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_44(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_46(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_45(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_47(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_46(T_, t_, __VA_ARGS__))
#define MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_48(T_, t_, n_, ...) \
    MARSHALLING_TRANSPORT_FIELD_ACC_FUNC(T_, t_, n_)             \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_47(T_, t_, __VA_ARGS__))

#define MARSHALLING_CHOOSE_TRANSPORT_FIELD_ACC_FUNC_(N, T_, t_, ...) \
    MARSHALLING_EXPAND(MARSHALLING_TRANSPORT_FIELD_ACC_FUNC_##N(T_, t_, __VA_ARGS__))
#define MARSHALLING_CHOOSE_TRANSPORT_FIELD_ACC_FUNC(N, T_, t_, ...) \
    MARSHALLING_EXPAND(MARSHALLING_CHOOSE_TRANSPORT_FIELD_ACC_FUNC_(N, T_, t_, __VA_ARGS__))
#define MARSHALLING_DO_TRANSPORT_FIELD_ACC_FUNC(T_, t_, ...) \
    MARSHALLING_EXPAND(                                      \
        MARSHALLING_CHOOSE_TRANSPORT_FIELD_ACC_FUNC(MARSHALLING_NUM_ARGS(__VA_ARGS__), T_, t_, __VA_ARGS__))

#define MARSHALLING_DEFINE_TRANSPORT_FIELD_ENUM(...) \
    MARSHALLING_EXPAND(MARSHALLING_DEFINE_ENUM(TransportFieldIdx, __VA_ARGS__));

#endif    // MARSHALLING_TRANSPORT_FIELDS_ACCESS_HPP
